﻿<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type='text/xsl' href='/expert/Xsl/2.xsl'?>
<Topic>
	<Issue>
		<PostUserNickName></PostUserNickName>
		<rank>一级(初级)</rank>
		<ranknum>user1</ranknum>
		<credit>100</credit>
		<TopicId>5582978</TopicId>
		<TopicName>怎么用控制台判断输入的只能是数字?</TopicName>
		<PostUserId>1665224</PostUserId>
		<PostUserName>No4000</PostUserName>
		<RoomName>.NET技术  C#</RoomName>
		<ReplyNum>1</ReplyNum>
		<PostDateTime>2007-6-5 20:52:34</PostDateTime>
		<Point>20</Point>
		<ReadNum>16</ReadNum>
		<RoomId>5201</RoomId>
		<EndState>0</EndState>
		<Content>
			<![CDATA[
我想用控制台只能输入数字,其他字符输入时给以报错,请问怎么写代码?
]]>
		</Content>
	</Issue>
	<Replys>
		<Reply><PostUserNickName>·￡孙子兵法￡·</PostUserNickName><rank>四星(高级)</rank><ranknum>star4</ranknum><credit>100</credit><ReplyID>41179526</ReplyID><TopicID>5582978</TopicID><PostUserId>420437</PostUserId><PostUserName>hertcloud</PostUserName><Point>0</Point><Content>using System;

namespace ConsoleApplication1
{
	/// &lt;summary&gt;
	/// Class1 的摘要说明。
	/// &lt;/summary&gt;
	class Class1
	{
		/// &lt;summary&gt;
		/// 判断字符是否为数字
		/// &lt;/summary&gt;
		/// &lt;param name="_strNum"&gt;传入的字符串&lt;/param&gt;
		/// &lt;returns&gt;boolean&lt;/returns&gt;
		public static bool isNumeric(string _strNum)
		{
			if (_strNum == null || _strNum == string.Empty)
			{
				Console.WriteLine("你输入的并非数字,请重新输入!");
				return false;
			}
			for (int i = 0; i &lt; _strNum.Length; i++)
			{
				if (!Char.IsNumber(_strNum, i))
				{
					Console.WriteLine("你输入的并非数字,请重新输入!");
					return false;
				}
			}
			return true; 
		}

		
		/// &lt;summary&gt;
		/// 应用程序的主入口点。
		/// &lt;/summary&gt;
		[STAThread]
		static void Main(string[] args)
		{
			string i;
			do 
			{
				i = Console.ReadLine();
				
			} while (!isNumeric(i));

			Console.WriteLine("\r\n你输入的数字为: "+i);

			Console.Read();
		}
	}
}
</Content><PostDateTime>2007-06-05 21:23:42</PostDateTime></Reply></Replys>
</Topic>
